package com.liveinstars.common.search;

import com.liveinstars.common.acframework.trie.Trie;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by guoyanjiang on 2019-03-26.
 */
public interface BaseSearchReq {

    String getContent();

    List<String> provideSearchKeys();

    //提供简单的分词方法
    default List<String> simpleSplitSearchKeys() {
        if (StringUtils.isEmpty(getContent())) {
            return Collections.emptyList();
        }
        List<String> keys = null;
        if (getContent().contains(",")) {
            String[] split = getContent().split(",");
            keys = Arrays.asList(split);
        } else if (getContent().contains(" ")) {
            String[] split = getContent().split(" ");
            keys = Arrays.asList(split);
        } else {
            keys = Arrays.asList(getContent());
        }
        return keys;
    }

    default Trie provideSearchTrie(){
        List<String> keys = provideSearchKeys();
        Trie trie = new Trie(false);
        for (String key : keys) {
            trie.addKeyword(key.toUpperCase());
        }
        return trie;
    }


}
